<template>
  <!--禁用工具栏，可以自定义-->
  <div style="z-index: 1; margin-right: 20px; margin-left: 209px; position: absolute">
    <div class="opt-style-text">
      <div @click="delFlowData" style="color: #e60505">
        <i class="iconfont icon-shanchu"></i> 清空流程
      </div>
      <div @click="delSelectNodeData" style="color: #e60505">
        <i class="iconfont icon-shanchujiedian"></i> 删除选中节点
      </div>
      <div @click="delSelectEdgesData" style="color: #e60505">
        <i class="iconfont icon-shanchujiedian1"></i> 删除选中连线
      </div>
      <div @click="revoke" style="color: #e6a23c">
        <i class="iconfont icon-fanhui"></i> 撤销
      </div>
      <div @click="backCenter" style="color: green">
        <i class="iconfont icon-huidaozhongxindian"></i>回到中心位置
      </div>
      <div @click="downloadImage" style="color: black">
        <i class="iconfont icon-daochutupian"></i>导出图片
      </div>
      <div @click="getFlowData">获取流程数据</div>
      <div @click="setFlowData">
        <i class="iconfont icon-tishi-shili"></i>
        设置流程数据
      </div>
      <div style="margin-left: 10px">
        设计模式:
        <label class="switch">
          <input type="checkbox" @change="switchPreviewState" />
          <span class="slider round">{{ switchText }}</span>
        </label>
      </div>
    </div>
  </div>
  <dawnFlow
    :ToolBar="false"
    ref="dawnFlowRef"
    @currentNodeDoubleClick="currentNodeClick"
    @currentEdgeDoubleClick="currentEdgeClick"
  ></dawnFlow>
</template>

<script setup>
import { ref } from "vue";
const dawnFlowRef = ref(null);
const isPreview = ref(false);
const switchText = ref("设计模式");

//导出图片
const downloadImage = () => {
  dawnFlowRef.value.downloadImageClick();
};

//获取流程数据
const getFlowData = () => {
  const flowData = dawnFlowRef.value.getFlowJson();
  alert(flowData);
};

//设置流程数据
const setFlowData = () => {
  dawnFlowRef.value.setFlowJson(
    '{"nodes":[{"id":"n_8112510918_1","type":"input","initialized":false,"position":{"x":110,"y":185},"data":{"label":"张三请假申请"},"class":"start-style","targetPosition":"left","sourcePosition":"right","style":{"zIndex":1}},{"id":"n_5770656858_2","type":"ordinaryNode","initialized":false,"position":{"x":330,"y":185},"data":{"label":"上级领导"},"class":"ordinaryNode-style","targetPosition":"left","sourcePosition":"right","style":{"zIndex":1}},{"id":"n_1288451677_3","type":"ordinaryNode","initialized":false,"position":{"x":640,"y":320},"data":{"label":"主管审批"},"class":"ordinaryNode-style","targetPosition":"left","sourcePosition":"right","style":{"zIndex":1}},{"id":"n_8376651849_4","type":"ordinaryNode","initialized":false,"position":{"x":645,"y":45},"data":{"label":"总经理审批"},"class":"ordinaryNode-style","targetPosition":"left","sourcePosition":"right","style":{"zIndex":1}},{"id":"n_5373814333_5","type":"ordinaryNode","initialized":false,"position":{"x":925,"y":195},"data":{"label":"人事经理审批"},"class":"ordinaryNode-style","targetPosition":"left","sourcePosition":"right","style":{"zIndex":1}},{"id":"n_6383009139_6","type":"output","initialized":false,"position":{"x":1145,"y":195},"data":{"label":"归档"},"class":"output-style","targetPosition":"left","sourcePosition":"right","style":{"zIndex":1}}],"edges":[{"id":"l_1624955486_0","type":"smoothstep","source":"n_8112510918_1","target":"n_5770656858_2","sourceHandle":null,"targetHandle":null,"data":{},"label":"","sourceX":233,"sourceY":204.5,"targetX":327,"targetY":204.5},{"id":"l_9691628419_3","type":"simplebezier","source":"n_5770656858_2","target":"n_8376651849_4","sourceHandle":null,"targetHandle":null,"data":{},"label":"请假天数>3天","sourceX":453,"sourceY":204.5,"targetX":642,"targetY":64.5},{"id":"l_2554200042_4","type":"simplebezier","source":"n_5770656858_2","target":"n_1288451677_3","sourceHandle":null,"targetHandle":null,"data":{},"label":"请假天数<=3天","sourceX":453,"sourceY":204.5,"targetX":637,"targetY":339.5},{"id":"l_6385971808_5","type":"simplebezier","source":"n_1288451677_3","target":"n_5373814333_5","sourceHandle":null,"targetHandle":null,"data":{},"label":"","sourceX":763,"sourceY":339.5,"targetX":922,"targetY":214.5},{"id":"l_1531781022_6","type":"simplebezier","source":"n_8376651849_4","target":"n_5373814333_5","sourceHandle":null,"targetHandle":null,"data":{},"label":"","sourceX":768,"sourceY":64.5,"targetX":922,"targetY":214.5},{"id":"l_7169921639_7","type":"simplebezier","source":"n_5373814333_5","target":"n_6383009139_6","sourceHandle":null,"targetHandle":null,"data":{},"label":"","sourceX":1048,"sourceY":214.5,"targetX":1142,"targetY":214.5}],"position":[-3,126],"zoom":1,"viewport":{"x":-3,"y":126,"zoom":1}}'
  );
};

//清空流程数据
const delFlowData = () => {
  dawnFlowRef.value.delFlowClick();
};

//删除选中节点数据（按住ctrl+鼠标左键）可以多选删除
const delSelectNodeData = () => {
  dawnFlowRef.value.delSelectNodeClick();
};

//删除选中连线数据（按住ctrl+鼠标左键）可以多选删除
const delSelectEdgesData = () => {
  dawnFlowRef.value.delSelectEdgesClick();
};

//回到中心位置
const backCenter = () => {
  dawnFlowRef.value.backCenterClick();
};

//撤销
const revoke = () => {
  dawnFlowRef.value.revokeClick();
};

//切换预览模式
const switchPreviewState = (event) => {
  isPreview.value = event.target.checked;
  if (isPreview.value) switchText.value = "预览模式";
  else switchText.value = "设计模式";
  dawnFlowRef.value.setPreviewState(isPreview.value);
};

//当前节点双击事件
const currentNodeClick = (nodeData) => {
  alert("你双击了当前节点，获得当前节点数据：" + JSON.stringify(nodeData));
};

//当前连线双击事件
const currentEdgeClick = (edgeData) => {
  alert("你双击了当前连线，获得当前连线数据：" + JSON.stringify(edgeData));
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.opt-style-text {
  min-width: 600px;
  margin-left: 5px;
  margin-top: 5px;
  display: flex;
  height: auto;
  min-height: 30px;
  color: #fff;
  font-weight: 700;
  border-right: 1px solid #eee;
  font-size: 12px;
  background-color: white;
  background: white;
  z-index: 1;
  -webkit-box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.3);
  box-shadow: 0 5px 10px #0000004d;
  flex-wrap: wrap;
  width: inherit;
}
.opt-style-text > div {
  margin-left: 10px;
  margin-right: 10px;
  margin-bottom: 5px;
  color: blue;
  display: flex;
  justify-content: center;
  align-items: center;
  /* pointer-events: auto; */
  cursor: pointer !important;
}

.switch {
  position: relative;
  display: inline-block;
  width: 85px;
  height: 18px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #13ce66;
  transition: 0.4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 13px;
  width: 13px;
  left: 4px;
  bottom: 3px;
  background-color: white;
  transition: 0.4s;
}

input:checked + .slider {
  background-color: #061ce2;
}

input:checked + .slider:before {
  transform: translateX(65px);
}

.slider.round {
  border-radius: 34px;
  color: white;
}

.slider.round:before {
  border-radius: 50%;
}
</style>
